package cn.harvest.storage.bo.manager;

import java.util.List;

import cn.harvest.storage.dao.DAO;
import cn.harvest.storage.po.Supplier;

public class SupplierService {

	private DAO dao = DAO.getDAO();

	private SupplierService() {

	}

	public static SupplierService getService() {

		SupplierService service = new SupplierService();
		return service;
	}

	public boolean create(Supplier supplier) {

		return dao.create(supplier);
	}
	
	public boolean update(Supplier supplier) {
		
		return dao.update(supplier);
	}

	public List selectBySQL(Supplier supplier) {

		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT s.supplier_id, s.supplier_name, s.supplier_man, s.supplier_phone, s.supplier_fax,"); 
		sql.append("        s.supplier_address, s.supplier_comment, s.supplier_type");
		sql.append(" FROM supplier s");
		sql.append(" WHERE 1=1");
		if (!(null == supplier.getSupplierId() || "".equals(supplier.getSupplierId()))) {
			sql.append(" AND s.supplier_id = '" + supplier.getSupplierId() + "' ");
		}
		if (!(null == supplier.getSupplierName() || "".equals(supplier.getSupplierName()))) {
			sql.append(" AND s.supplier_name LIKE '%" + supplier.getSupplierName() + "%' ");
		}
		if (!(null == supplier.getSupplierMan() || "".equals(supplier.getSupplierMan()))) {
			sql.append(" AND s.supplier_man LIKE '%" + supplier.getSupplierMan() + "%' ");
		}
		if (!(null == supplier.getSupplierType() || "".equals(supplier.getSupplierType()))) {
			sql.append(" AND s.supplier_type = '" + supplier.getSupplierType() + "' ");
		}
		if (!(null == supplier.getSupplierComment() || "".equals(supplier.getSupplierComment()))) {
			sql.append(" AND s.supplier_comment = '" + supplier.getSupplierComment() + "' ");
		}
		sql.append(" ORDER BY s.supplier_id");

		System.out.println("### - SupplierService -> selectBySQL -> #SQL \r\n	 " + sql);
		
		return dao.selectBySQL(sql.toString());
	}
	
	public Supplier selectByHQL(String supplierId) {
		StringBuffer hql = new StringBuffer();
		hql.append(" FROM Supplier s WHERE s.supplierId = " + supplierId);
		
		System.out.println("### - SupplierService -> selectByHQL -> #HQL \r\n	 " + hql);
		
		Supplier supplier = (Supplier) dao.selectByHQL(hql.toString()).get(0);
		return supplier;
	}
	
	public List selectWarehouseKeeper() {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT em.employee_id,em.employee_name FROM employee em WHERE em.employment_id = -1");
		
		System.out.println("### - SupplierService -> selectWarehouseKeeper -> #SQL \r\n	 " + sql);
		
		List keeperList = dao.selectBySQL(sql.toString());
		return keeperList;
	}
	
	
	public static void main(String[] args) {

	}
}
